﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class FeedbackUI
    {
        private const String CACHE_KEY = "Feedback_GetAll";

        public int Add(int CategoryPageID, String Email, String Title,
            String FeedbackContent, String FeedbackIP, DateTime FeedbackDate, bool IsApproved)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Feedback_add", (CategoryPageID == -1?null:(int?)CategoryPageID),  Email,  Title,
              FeedbackContent,  FeedbackIP,  FeedbackDate,  IsApproved);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int FeedbackID, int CategoryPageID, String Email, String Title,
            String FeedbackContent, String FeedbackIP, DateTime FeedbackDate, bool IsApproved)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Feedback_update", FeedbackID, CategoryPageID, Email, Title,
                FeedbackContent, FeedbackIP, FeedbackDate, IsApproved);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + FeedbackID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int FeedbackID, bool IsApproved)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Feedback_updateStatus", FeedbackID, IsApproved);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + FeedbackID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Feedback_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int FeedbackID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + FeedbackID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Feedback_get", FeedbackID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + FeedbackID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int FeedbackID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Feedback_delete", FeedbackID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
